package com.newegg.hardware.benchmark.api;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.newegg.hardware.benchmark.model.BaseAPI;
import com.newegg.hardware.benchmark.model.HardwareScore;
import com.newegg.hardware.benchmark.model.Response;
import com.newegg.hardware.benchmark.model.Score;
import com.newegg.hardware.benchmark.model.Type;
import com.newegg.hardware.benchmark.service.ScoreService;

@RestController
@RequestMapping("/api/hardware")
@RefreshScope
public class HardwareAPI extends BaseAPI{
	@Autowired
	ScoreService scoreService;
	
	@Value("${hardware.nonabs.version}")
	String nonabs_version;
	@Value("${hardware.nonabs.version.url}")
	String nonabs_versionurl;
	
	@Value("${hardware.abs.version}")
	String abs_version;
	@Value("${hardware.abs.version.url}")
	String abs_versionurl;
	
	@GetMapping("/findByName")
    public Response<Score> findByName(@RequestParam Type type, @RequestParam String name, HttpServletRequest request, HttpServletResponse response) throws Exception {
		return SUCCESS(scoreService.findByName(type, name));
    }
	
	@GetMapping("/findByItem")
    public Response<Score> findByItem(@RequestParam String itemnumber, HttpServletRequest request, HttpServletResponse response) throws Exception {
		return SUCCESS(scoreService.findByItem(itemnumber));
    }
	
    @PostMapping("/score")
    public Response<HardwareScore> score(@RequestBody JSONObject json, @RequestHeader Map<String, String> headers, HttpServletRequest request, HttpServletResponse response) throws Exception {
		return SUCCESS(scoreService.getScore(json, headers));
    }
    
    @PostMapping("/report")
    public Response<Void> report(@RequestBody Object obj,  @RequestHeader Map<String, String> headers, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	scoreService.report(obj, headers);
		return SUCCESS();
    }
    
    @GetMapping("/version")
    public Response<Map<String, String>> version(@RequestParam(required=false, defaultValue="DEFAULT") String type) {
    	if(type.toLowerCase().equals("abs")) {
    		Map<String, String> map = new HashMap<String, String>();
        	map.put("version", abs_version);
        	map.put("url", abs_versionurl);
    		return SUCCESS(map);
    	}else {
    		Map<String, String> map = new HashMap<String, String>();
        	map.put("version", nonabs_version);
        	map.put("url", nonabs_versionurl);
    		return SUCCESS(map);
    	}
    }
}
